﻿
using System.Collections.Generic;
using System.Data;
using System;
using ZHDJ.MobileInterface.Common;
namespace ZHDJ.MobileInterface
{
    public partial class ToWanDonations : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            ToWanDonationsOutParam result = new ToWanDonationsOutParam();
            ToWanDonationsInParam param = new ToWanDonationsInParam();
            string logMsg = "";
            bool isFlag = true;
            string branchID = "";
            string branchName = "";
            string loginName = "";
                 
            try
            {  
                param = inParam as ToWanDonationsInParam;

                if (string.IsNullOrEmpty(param.LoginID))
                {
                    result.code = "0";
                    result.desc = "登录ID不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (param.Titular != "0" && param.Titular != "1" && param.Titular != "2")
                    {
                        result.code = "0";
                        result.desc = "捐助名义必须为:0 本人；1 组织；2 匿名";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (param.Titular == "0" && (string.IsNullOrEmpty(param.CreatebyContact) || string.IsNullOrEmpty(param.CreatebyAddress)))
                    {
                        result.code = "0";
                        result.desc = "捐助名义为本人时，联系方式以及联系地址不能为空";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (param.Titular == "1" && (string.IsNullOrEmpty(param.BranchName) || string.IsNullOrEmpty(param.BranchContactPerson) 
                        || string.IsNullOrEmpty(param.BranchContact) || string.IsNullOrEmpty(param.BranchAddress)))
                    {
                        result.code = "0";
                        result.desc = "捐助名义为组织时，组织名称、组织联系人、组织联系人电话、组织地址不能为空";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (param.ThingType != "0" && param.ThingType != "1" && param.ThingType != "2" && param.ThingType != "3")
                    {
                        result.code = "0";
                        result.desc = "捐助类型必须为：0 款项；1 物品；2 服务；3 其他";

                        isFlag = false;
                    }
                    else if (param.ThingType == "0" )
                    { param.Name = "资助金"; }
                    else if (param.ThingType == "1" || param.ThingType == "3")
                    {
                        if (string.IsNullOrEmpty(param.Name))
                        {
                            result.code = "0";
                            result.desc = "捐助类型为物品或者其他时，捐助名称不能为空";

                            isFlag = false;
                        }
                    }
                    else if (param.ThingType == "2")
                    {
                        if (param.Name != "0" && param.Name != "1" && param.Name != "2" && param.Name != "3" && param.Name != "4"
                             && param.Name != "5" && param.Name != "6" && param.Name != "7" && param.Name != "8" && param.Name != "9")
                        {
                            result.code = "0";
                            result.desc = "捐助类型为服务时，捐助名称必须为：0 特殊教育；1 文化辅导；2 医疗辅导；3 法律援助；4 科技服务；5 绿化环保；6 陪护老幼；7 陪护病人；8 照顾残障；9 心里咨询";

                            isFlag = false;
                        }
                    }
                }

                int ThingCountInt = 0;
                DateTime EndTimeDt = DateTime.Now;

                if (isFlag)
                {
                    try
                    {
                        ThingCountInt = Convert.ToInt32(param.ThingCount);
                    }
                    catch
                    {
                        result.code = "0";
                        result.desc = "数量必须为整型数据";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    try
                    {
                        EndTimeDt = Convert.ToDateTime(param.EndTime);
                    }
                    catch
                    {
                        result.code = "0";
                        result.desc = "有效期不是有效日期格式数据";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (param.SaveModus != "0" && param.SaveModus != "1")
                    {
                        result.code = "0";
                        result.desc = "捐助形式必须为：0 爱心ATM；1 上门取";

                        isFlag = false;
                    }
                } 

                if (isFlag)
                {
                    if (param.Purposes != "0" && param.Purposes != "1" && param.Purposes != "2" && param.Purposes != "3")
                    {
                        result.code = "0";
                        result.desc = "资金用途必须为：0 低保、残疾、特困等居民；1 自助申请的困难居民；2 指定用途（请列明）；3 任意用途";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (param.Purposes == "2" && string.IsNullOrEmpty(param.PurposesDescription))
                    {
                        result.code = "0";
                        result.desc = "资金用途为指定用途时用途说明不能为空";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    DataSet dsLoginName = InterfaceHelper.GetUseInfo(param.LoginID);
                    if (dsLoginName == null || dsLoginName.Tables[0].Rows.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "当前登陆ID无效或者不存在";

                        isFlag = false;
                    }
                    else
                    {
                        loginName = dsLoginName.Tables[0].Rows[0]["userName"].ToString();
                    }
                }

                if (isFlag)
                {
                    // 获取组织ID
                    branchName = param.BranchName;
                    DataSet ds = InterfaceHelper.GetBranchInfo(param.BranchName);
                    if(ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        branchID = Convert.ToString(ds.Tables[0].Rows[0]["BranchID"]);
                        branchName = Convert.ToString(ds.Tables[0].Rows[0]["BranchName"]);
                    }

                    ZHDJ.MobileInterface.Common.IWanDonations.FieldAttribute FA = new Common.IWanDonations.FieldAttribute()
                    {
                        BranchAddress = param.BranchAddress,
                        BranchContact = param.BranchContact,
                        BranchContactPerson = param.BranchContactPerson,
                        BranchID = branchID,
                        BranchName = branchName,
                        Createby = loginName,
                        CreatebyAddress = param.CreatebyAddress,
                        CreatebyContact = param.CreatebyContact,
                        CreateTime = DateTime.Now,
                        Description = param.Description,
                        EndTime = EndTimeDt,
                        Name = NameParse(param.ThingType, param.Name),
                        Purposes = param.Purposes,
                        PurposesDescription = param.PurposesDescription,
                        SaveModus = param.SaveModus,
                        ThingCount = ThingCountInt,
                        ThingType = param.ThingType,
                        Titular = param.Titular 
                    };

                    string rfResult = IWanDonations.Donations(param.LoginID, FA); 
                    if (!string.IsNullOrEmpty(rfResult))
                    {
                        result.code = "0";
                        result.desc = rfResult;

                        isFlag = false;
                    }
                } 
                 
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "转移过程出现异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }
    
            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-ToWanDonations", SerializeHelper.JsonSerializer<ToWanDonationsInParam>(param), logMsg); 
            return result as OutParam;
        } 

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<ToWanDonationsInParam, ToWanDonationsOutParam>();
            else
                SafeProcessRequest<ToWanDonationsInParam, ToWanDonationsOutParam>();
        }

        private string NameParse(string type, string name)
        {
            if (type != "2")
                return name;

            //（0:特殊教育1：文化辅导2:医疗辅导3：法律援助4:科技服务5：绿化环保6:陪护老幼7：陪护病人8：照顾残障9:心里咨询）
            switch (name)
            {
                case "0":
                    return "特殊教育";
                case "1":
                    return "文化辅导";
                case "2":
                    return "医疗辅导";
                case "3":
                    return "法律援助";
                case "4":
                    return "科技服务";
                case "5":
                    return "绿化环保";
                case "6":
                    return "陪护老幼";
                case "7":
                    return "陪护病人";
                case "8":
                    return "照顾残障";
                case "9":
                    return "心里咨询";
                default:
                    return "";
            }
        }
    }

    #region 请求参数
    public class ToWanDonationsInParam
    { 
        /// <summary>
        /// 登录ID	string	必须
        /// </summary>
        public string LoginID
        { get; set; } 
	
        /// <summary>
        /// 捐助名义	String	0：本人
        /// 1：组织
        /// 2：匿名
        /// </summary>
        public string Titular
        { get; set; }	

        /// <summary>
        /// 联系方式	string	捐助名义为本人时必须，其他情况不传
        /// </summary>
        public string CreatebyContact
        { get; set; }	

        /// <summary>
        /// 联系地址	string	捐助名义为本人时必须，其他情况不传
        /// </summary>
        public string CreatebyAddress
        { get; set; }
        
        /// <summary>
        /// 组织名称	String	捐助名义为“组织”时候，必传
        /// </summary>
        public string BranchName
        { get; set; }
         
        /// <summary>
        /// 组织联系人	String	捐助名义为“组织”时候，必传
        /// </summary>
        public string BranchContactPerson
        { get; set; }
        	
        /// <summary>
        /// 组织联系人电话	String	捐助名义为“组织”时候，必传
        /// </summary>
        public string BranchContact
        { get; set; }
        	
        /// <summary>
        /// 组织地址	String	捐助名义为“组织”时候，必传
        /// </summary>
        public string BranchAddress
        { get; set; }
        	
        /// <summary>
        /// 捐助类型	
        /// String	0：款项
        /// 1：物品
        /// 2：服务
        /// 3：其他
        /// </summary>
        public string ThingType
        { get; set; }
        	
        /// <summary>
        /// 名称	String	1：捐助类型为0时，不传
        /// 2：捐助类型为1或3时，必传
        /// 3：捐助类型为2时，必传对应值为（0:特殊教育1：文化辅导2:医疗辅导3：法律援助4:科技服务5：绿化环保6:陪护老幼7：陪护病人8：照顾残障9:心里咨询）
        /// </summary>
        public string Name
        { get; set; }
        	
        /// <summary>
        /// 数量	Int	必填
        /// </summary>
        public string ThingCount
        { get; set; }
        	
        /// <summary>
        /// 有效期	DateTime	必填
        /// </summary>
        public string EndTime
        { get; set; }
        	
        /// <summary>
        /// 捐助形式	String	0：爱心ATM  1：上门取
        /// </summary>
        public string SaveModus
        { get; set; }
        	
        /// <summary>
        /// 资金用途	String	0：低保、残疾、特困等居民  1：自助申请的困难居民2：指定用途（请列明）  3：任意用途
        /// </summary>
        public string Purposes
        { get; set; }
        	 
        /// <summary>
        /// 用途说明	String	当资金用途选择‘2’时，必填
        /// </summary>
        public string PurposesDescription
        { get; set; }
        	
        /// <summary>
        /// 描述	String	必填
        /// </summary>
        public string Description
        { get; set; }
        	

    }
    #endregion

    #region 结果参数
    public class ToWanDonationsOutParam
    {
        public ToWanDonationsOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; } 
    } 
    #endregion 
}